package com.wangzemin.leetcode.num62;


public class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if (head == null || head.next == null || k == 0) {
            return head;
        }
        ListNode tail = head;
        int length = 1;
        while (tail.next != null) {
            tail = tail.next;
            length++;
        }
        int moveLength = length - k % length;
        tail.next = head;
        for (int i = 0; i < moveLength; i++) {
            tail = tail.next;
        }
        ListNode rotateHead = tail.next;
        tail.next = null;
        return rotateHead;
    }
}
class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}